跳到主要内容

监听器动作脚本(Listener Action Scripts)

监听器(Listeners) 会在状态机特定事件发生时触发。

监听器动作脚本(Listener Action Scripts)允许你在该时刻执行自定义逻辑(副作用)。

创建监听器动作脚本

创建新脚本 并选择 监听器动作(Listener Action Script)

脚本结构(Anatomy)

type MyListenerAction = {
context: Context,
}

-- Called once when the script initializes.
function init(self: MyListenerAction, context: Context): boolean
-- Context gives you access to your main view model and other data.
self.context = context
return true
end

-- Called when the Listener fires.
-- Use this to perform side effects (no return value).
function perform(self: MyListenerAction, pointerEvent: PointerEvent)

end

-- Return a factory function that Rive uses to build the Listener Action instance.
return function(): ListenerAction<MyListenerAction>
return {
init = init,
perform = perform,
context = late(),
}
end

添加监听器动作

  1. 选中一个监听器(Listener)
  2. 点击 + 并选择 Scripted Action
  3. 在 Run 下拉中选择你的脚本

Add a custom listener action

脚本输入(Script Inputs)

输入(Inputs)可为监听器动作传参,使同一脚本可复用于多个监听器。

说明(Note) 输入可以控制脚本,但脚本不能修改输入值。

若需要控制视图模型属性,请通过上下文(Context)或视图模型输入(View Model Inputs)访问。

设置输入

点击监听器脚本旁的属性图标(Properties icon)设置输入值。

listener action script inputs

绑定输入

右键属性并选择 Data Bind,把输入绑定到视图模型属性。

listener action script inputs